<?php

//积分记录模型
class MembersActionModel extends BaseModel {

    //登录类型记录
    const action_login = 0;
    //支付充值
    const action_alipay = 1;
    //系统
    const action_system = 2;
    //分享获取积分
    const action_share = 3;
    //积分兑换
    const action_redeem = 4;
    //举报
    const action_punish = 5;
    //邀请注册送积分
    const action_invite = 6;

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{members_action}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('uid', 'required', 'message' => '用户UID不能为空！'),
            array('note', 'required', 'message' => '记录不能为空！'),
            array('action,action_time', 'safe'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'uid' => '用户ID',
            'action' => '0为赠送积分',
            'note' => '备注',
            'action_time' => '动作发生时间',
        );
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return MembersAction the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * 增加一条支付宝积分记录
     * @param type $uid 用户ID
     * @param type $note 说明
     * @return type
     */
    public function addAlipayAction($uid, $note) {
        return $this->addAction($uid, self::action_alipay, $note);
    }

    /**
     * 增加一条登录积分记录
     * @param type $uid 用户ID
     * @param type $note 说明
     * @return boolean
     */
    public function addLoginAction($uid, $note = '每日登录送 5 个积分。') {
        return $this->addAction($uid, self::action_login, $note);
    }

    /**
     * 积分兑换记录
     * @param type $uid 用户UID
     * @param type $note 消息
     * @return boolean
     */
    public function addRedeemAction($uid, $note) {
        return $this->addAction($uid, self::action_redeem, $note);
    }

    /**
     *  分享链接获取积分记录
     * @param type $uid 用户UID
     * @param type $note 消息
     * @return boolean
     */
    public function addShareAction($uid, $note) {
        return $this->addAction($uid, self::action_share, $note);
    }

    /**
     * 邀请好友注册送积分
     * @param type $uid 用户UID
     * @param type $note 消息
     * @return type
     */
    public function addInviteAction($uid, $note = '邀请好友注册赠送 50 积分。') {
        return $this->addAction($uid, self::action_invite, $note);
    }

    /**
     * 增加一条积分变更记录
     * @param type $uid 用户UID
     * @param type $action 类型
     * @param type $note 说明
     * @return boolean
     */
    public function addAction($uid, $action, $note) {
        $data = array(
            'uid' => $uid,
            'action' => $action,
            'note' => $note,
        );
        $this->attributes = $data;
        $this->setIsNewRecord(true);
        if ($this->validate() && $this->save()) {
            return true;
        }
        return false;
    }

    //获取积分记录
    public function getScores($uid, $page_num = 20) {
        $uid = (int) $uid;
        if (!$uid) {
            return false;
        }

        $criteria = new CDbCriteria();
        $criteria->addCondition('uid = :uid');
        $criteria->params = array(
            ':uid' => $uid,
        );
        $criteria->order = 'action_time desc';
        $page_num = (int) $page_num < 0 ? 15 : $page_num;

        $count = $this->model()->count($criteria);
        $pager = new CPagination($count);
        $pager->pageSize = $page_num;
        $pager->applyLimit($criteria);
        $scores = $this->findAll($criteria);
        return array($scores, $pager);
    }

    /**
     * 获取当天用户分享链接获取积分数量
     * @param type $uid 用户ID
     * @return int
     */
    public function getThatdayShareSum($uid) {
        if (empty($uid)) {
            return 0;
        }
        $count = $this->count($this->where(array(
                    'uid' => $uid,
                    'action' => self::action_share,
                    'action_time' => array('GT', strtotime(date('Y-m-d 00:00:00'))),
        )));
        return $count > 0 ? $count : 0;
    }

    //数据保存前操作
    protected function beforeSave() {
        //是否新增
        if ($this->isNewRecord) {
            $this->action_time = time();
        } else {
            
        }
        return true;
    }

}
